package com.novatec.dao;

import java.text.ParseException;

import javax.sql.DataSource;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.simple.SimpleJdbcTemplate;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

public class UtilsDAO {
	
	@SuppressWarnings("unused")
	private final Logger logger = Logger.getLogger(this.getClass());
	private SimpleJdbcTemplate simpleJdbcTemplate;
//	private SimpleDateFormat sdf = new SimpleDateFormat("dd/MM/yyy");

	@Autowired
    public void init(DataSource dataSource) {
        this.simpleJdbcTemplate = new SimpleJdbcTemplate(dataSource);
    }

	@Transactional(readOnly = false, propagation = Propagation.REQUIRED)
	public Long getID(String tableName) throws ParseException {
		
		String sqlUpdate = "UPDATE identificadores SET id = LAST_INSERT_ID(id + 1) WHERE name = ?";
		String sqlSelect = "SELECT LAST_INSERT_ID()";
		
		simpleJdbcTemplate.update(sqlUpdate, tableName);

		return simpleJdbcTemplate.queryForLong(sqlSelect);
	
	}
	

}
